package com.house365.core.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.house365.core.R;
import com.house365.core.image.ImageUtil;
/**
 * 调用DEMO
 * 
 *ConfirmDialog.Builder dialogBuilder = new ConfirmDialog.Builder(
						VoteActivity.this.getParent());
				dialogBuilder.setTitle("提示信息");
				dialogBuilder.setMessage("提醒内容信息");
				dialogBuilder.setNegativeButton(R.string.dialog_button_cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						});
				dialogBuilder.setPositiveButton(R.string.dialog_button_ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						});
				dialogBuilder.create().show();
 */
public class ConfirmDialog extends Dialog {
 
	public ConfirmDialog(Context context) {
		super(context);
	}

	public ConfirmDialog(Context context, int theme) {
		super(context, theme);
	} 

	public static class Builder {

		private Context context;
		private String title;
		private String message;
		private String positiveButtonText;
		private String negativeButtonText;
		private String messagePic;
		private Bitmap messagePicBitmap;
		private int messagePicWidth,messagePicHeight;
		private DialogInterface.OnClickListener positiveButtonClickListener,
				negativeButtonClickListener;

		public Builder(Context context) {
			this.context = context;
		} 

		/**
		 * Set the Dialog message from String
		 * 
		 * @param title
		 * @return
		 */
		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}
		
		public Builder setMessagePic(String pic) {
			this.messagePic = pic;
			return this;
		}
		public Builder setMessagePic(String pic,int messagePicWidth,int messagePicHeight) {
			this.messagePic = pic;
			this.messagePicWidth = messagePicWidth;
			this.messagePicHeight =  messagePicHeight;
			return this;
		}
		public Bitmap getMessagePicBitmap() {
			return messagePicBitmap;
		}

		public void setMessagePicBitmap(Bitmap messagePicBitmap) {
			this.messagePicBitmap = messagePicBitmap;
		}
		public Builder setMessagePicBitmap(Bitmap messagePicBitmap,int messagePicWidth,int messagePicHeight) {
			this.messagePicBitmap = messagePicBitmap;
			this.messagePicWidth = messagePicWidth;
			this.messagePicHeight =  messagePicHeight;
			return this;
		}
		/**
		 * Set the Dialog message from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 * 
		 * @param title
		 * @return
		 */
		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		/**
		 * Set a custom content view for the Dialog. If a message is set, the
		 * contentView is not added to the Dialog...
		 * 
		 * @param v
		 * @return
		 */
		public Builder setContentView(View v) {
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 * 
		 * @param positiveButtonText
		 * @param listener
		 * @return
		 */
		public Builder setPositiveButton(int positiveButtonText,
				DialogInterface.OnClickListener listener) {
			this.positiveButtonText = (String) context
					.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;
			return this;
		}

		/**
		 * Set the positive button text and it's listener
		 * 
		 * @param positiveButtonText
		 * @param listener
		 * @return
		 */
		public Builder setPositiveButton(String positiveButtonText,
				DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		/**
		 * Set the negative button resource and it's listener
		 * 
		 * @param negativeButtonText
		 * @param listener
		 * @return
		 */
		public Builder setNegativeButton(int negativeButtonText,
				DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context
					.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		/**
		 * Set the negative button text and it's listener
		 * 
		 * @param negativeButtonText
		 * @param listener
		 * @return
		 */
		public Builder setNegativeButton(String negativeButtonText,
				DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}

		/**
		 * Create the custom dialog
		 */
		public ConfirmDialog create() {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final ConfirmDialog dialog = new ConfirmDialog(context,
					R.style.dialog);
			View layout = inflater.inflate(R.layout.dialog_confirm, null);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			((TextView) layout.findViewById(R.id.dialog_title)).setText(title);
			if (positiveButtonText != null) {
				((Button) layout.findViewById(R.id.dialog_button_ok))
						.setText(positiveButtonText);
				if (positiveButtonClickListener != null) {
					((Button) layout.findViewById(R.id.dialog_button_ok))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									positiveButtonClickListener.onClick(dialog,
											DialogInterface.BUTTON_POSITIVE);
								}
							});
				}
			} else {
				layout.findViewById(R.id.dialog_button_ok).setVisibility(View.GONE);
			}
			// set the cancel button
			if (negativeButtonText != null) {
				((Button) layout.findViewById(R.id.dialog_button_cancel))
						.setText(negativeButtonText);
				if (negativeButtonClickListener != null) {
					((Button) layout.findViewById(R.id.dialog_button_cancel))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									negativeButtonClickListener.onClick(dialog,
											DialogInterface.BUTTON_NEGATIVE);
								}
							});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.dialog_button_cancel).setVisibility(View.GONE);
			}
			// set the content message
			if (message != null) {
				((TextView) layout.findViewById(R.id.dialog_message))
						.setText(message);
			} 
			if (messagePic != null) {
				ImageView imageView = (ImageView) layout.findViewById(R.id.dialog_message_pic);
				
				if(messagePicWidth>0 && messagePicHeight>0){
					android.view.ViewGroup.LayoutParams imageLayoutPrams = imageView.getLayoutParams();
					if(imageLayoutPrams == null){
						imageLayoutPrams = new LayoutParams(messagePicWidth, messagePicHeight);
					}else{
						imageLayoutPrams.width = messagePicWidth;
						imageLayoutPrams.height = messagePicHeight;
					}
				}
				imageView.setVisibility(View.VISIBLE);
				ImageUtil.asyncImg(messagePic, imageView, context);
				//((BaseCommonActivity)context).setImage(imageView, messagePic, R.drawable.img_default_small, AsyncImageLoader.IMAGE_SCALE_TYPE_MIDDLE);
				
			} else{
				if(messagePicBitmap != null && !messagePicBitmap.isRecycled()){
					ImageView imageView = (ImageView) layout.findViewById(R.id.dialog_message_pic);
					if(messagePicWidth>0 && messagePicHeight>0){
						android.view.ViewGroup.LayoutParams imageLayoutPrams = imageView.getLayoutParams();
						if(imageLayoutPrams == null){
							imageLayoutPrams = new LayoutParams(messagePicWidth, messagePicHeight);
						}else{
							imageLayoutPrams.width = messagePicWidth;
							imageLayoutPrams.height = messagePicHeight;
						}
					}
					imageView.setVisibility(View.VISIBLE);
					imageView.setImageBitmap(messagePicBitmap);
				}
			}
			dialog.setContentView(layout);
			return dialog;
		}

		  
	}

}
